package com.his.struts.dao.shebei;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.his.struts.dao.search.SearchImp;
import com.his.struts.form.ShebeiForm;
import com.his.struts.util.DbUtil;

/**
 * 医院设备
 * 
 * @author liumiaoyue
 * 
 */
public class SheBeiImp implements SheBeiDao {
	Logger log = Logger.getLogger(SheBeiImp.class);
	DbUtil db = new DbUtil();
	/**
	 * 得到设备
	 * 
	 * @returnList<ShebeiForm>设备集合
	 */
	public List<ShebeiForm> getShebei() {
		List<ShebeiForm> list = new ArrayList<ShebeiForm>();
		String sql = "select * from shebei order by sbId desc";
		ResultSet rs = db.executeQuery(sql, null);
		try {
			while (rs.next()) {
				ShebeiForm form = new ShebeiForm();
				form.setSbId(rs.getInt("sbId"));
				form.setSbName(rs.getString("sbName"));
				form.setSbNumber(rs.getInt("sbNumber"));
				form.setSbPic(rs.getString("sbPic"));
				form.setSbInfo(rs.getString("sbInfo"));
				list.add(form);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}

		return list;
	}
	/**
	 * 得到设备
	 * 
	 * @returnList<ShebeiForm>设备集合
	 */
	public List<ShebeiForm> getShebeiBean(ResultSet rs) {
		List<ShebeiForm> list = new ArrayList<ShebeiForm>();
		try {
			while (rs.next()) {
				ShebeiForm form = new ShebeiForm();
				form.setSbId(rs.getInt("sbId"));
				form.setSbName(rs.getString("sbName"));
				form.setSbNumber(rs.getInt("sbNumber"));
				form.setSbPic(rs.getString("sbPic"));
				form.setSbInfo(rs.getString("sbInfo"));
				list.add(form);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}

		return list;
	}

	/**
	 * 添加设备
	 * 
	 * @param form
	 *            设备bean
	 * @return boolean
	 */
	public boolean addShebei(ShebeiForm form) {
		boolean ok = false;
		String sql = "insert into shebei(sbName,sbNumber,sbInfo,sbPic) values(?,?,?,?) ";
		ArrayList list = new ArrayList();
		list.add(form.getSbName());
		list.add(form.getSbNumber());
		list.add(form.getSbInfo());
		list.add(form.getSbPic());
		int i = db.executeUpdate(sql, list);
		if (i != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}
	
	

	/**
	 * 删除设备
	 * 
	 * @param sbId设备id
	 * @return boolean
	 */
	public boolean delShebei(int sbId) {
		boolean ok = false;
		String sql = "delete from shebei where sbId=?";
		ArrayList list = new ArrayList();
		list.add(sbId);
		int i = db.executeUpdate(sql, list);
		if (i != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}

	/**
	 * 更新设备
	 * 
	 * @param form
	 *            设备bean
	 * @return boolean
	 */
	public boolean updateShebei(ShebeiForm form) {
		boolean ok = false;
		String sql = "update shebei set sbName=?,sbNumber=?,sbInfo=? where sbId=?";
		ArrayList list = new ArrayList();
		list.add(form.getSbName());
		list.add(form.getSbNumber());
		list.add(form.getSbInfo());
		if(form.getSbPic() !=null && !"".equals(form.getSbPic())){
			sql = "update shebei set sbName=?,sbNumber=?,sbInfo=?,sbPic=? where sbId=?";
			list.add(form.getSbPic());
		}
		list.add(form.getSbId());
		int i = db.executeUpdate(sql, list);
		if (i != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}

}
